﻿//==========================================================
// 此代码由TilverNET代码生成工具自动生成，请不要修改
// 代码生成器核心库版本号：1.2
// 作者：landy
// 联系方式：(QQ)86566178
// 创建时间：2013/8/25 21:28:41
//==========================================================

using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using Tilver.Data;
using Tilver.Data.DataPager;

namespace Tilver.DAL.TilverDB.LANDY
{
    #region EO

    /// <summary>
    ///  《LANDY.City(实体类)》
    /// </summary>
    [Serializable]
    public class CityEO : IRowMapper<CityEO>
    {
        protected bool _hasOriginal = false;
        protected int _cityId;
        protected int _originalCityId;
        protected string _code;
        protected string _name;

        /// <summary>
        /// 主键是否有原始值（即数据库中保存的值），用于实体对象更新时
        /// </summary>
        public bool HasOriginal
        {
            get { return _hasOriginal; }
        }


        /// <summary>
        ///  《[主键]  [int]   自增字段》
        /// </summary>
        public int CityId
        {
            get { return _cityId; }
            set { _cityId = value; }
        }

        /// <summary>
        ///  《数据库中的原始主键值副本》
        /// </summary>
        public int OriginalCityId
        {
            get { return _originalCityId; }
            set
            {
                _hasOriginal = true;
                _originalCityId = value;
            }
        }

        /// <summary>
        ///  《[字段]  [varchar]》
        /// </summary>
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

        /// <summary>
        ///  《[字段]  [nvarchar]》
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                if (value != null && value.Length > 50)
                    throw new ArgumentOutOfRangeException("Invalid value for Name", value, value == null ? "null" : value.ToString());
                _name = value;
            }
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CityEO()
        {
        }

        /// <summary>
        /// 自定义构造函数
        /// </summary>
        /// <param name = "cityId"></param>
        /// <param name = "code"></param>
        /// <param name = "name"></param>
        public CityEO(int cityId, string code, string name)
        {
            _cityId = cityId;
            _code = code;
            _name = name;
        }


        /// <summary>
        /// 将结果集映射成实体对象
        /// </summary>
        /// <param name = "reader">只进结果集流</param>
        /// <return></return>
        public CityEO MapRow(IDataReader reader)
        {
            var ret = new CityEO();
            ret.CityId = (int)reader["CityId"];
            ret.OriginalCityId = ret.CityId;
            ret.Code = (reader["Code"] is DBNull) ? null : (string)reader["Code"];
            ret.Name = (reader["Name"] is DBNull) ? null : (string)reader["Name"];
            return ret;
        }

        /// <summary>
        /// 将结果集映射成实体对象，返回映射方法
        /// </summary>
        /// <return></return>
        public Func<IDataReader, CityEO> GetMapper()
        {
            return MapRow;
        }


    }


    #endregion
    #region MO

    /// <summary>
    ///  《LANDY.City(逻辑类)》
    /// </summary>
    public class CityMO : DbManagerBase
    {

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name = "routeData">路由数据</param>
        public CityMO(object routeData = null) : base(routeData) { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name = "connectionStringName">连接字符串名称</param>
        public CityMO(string connectionStringName) : base(connectionStringName) { }

        #endregion

        #region 私有方法
        /// <summary>
        /// 数据库访问操作方法
        /// </summary>
        /// <param name = "sql">SQL语句</param>
        /// <return>返回数据操作对象</return>
        private DbAdoBase AdoBase(string sql)
        {
            return DataBase.CreateDbAdo(sql, CommandType.Text);
        }

        #endregion

        #region Select方法集合
        /// <summary>
        /// 按主键查询
        /// </summary>
        /// <param name = "cityId"></param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public CityEO SelectByPK(int cityId, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            sql.AppendFormat(@"SELECT [CityId], [Code], [Name] FROM [LANDY].[City] WHERE [CityId]=@CityId ");
            return AdoBase(sql.ToString())
                            .AddInParameter("@CityId", cityId, DbType.Int32, 4)
                            .ExecuteReader(tm).MapToSingle(new CityEO().GetMapper());
        }


        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "cityId"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<CityEO> SelectByCityId(int cityId, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} [CityId], [Code], [Name] FROM [LANDY].[City] WHERE [CityId]=@CityId");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@CityId", cityId, DbType.Int32, 4)
                            .ExecuteList(new CityEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "code"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<CityEO> SelectByCode(string code, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} [CityId], [Code], [Name] FROM [LANDY].[City] WHERE [Code]=@Code");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@Code", code, DbType.AnsiString, 10)
                            .ExecuteList(new CityEO().GetMapper(), tm);
        }
        /// <summary>
        /// 按字段查询
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
        /// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<CityEO> SelectByName(string name, int top, string sort, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} [CityId], [Code], [Name] FROM [LANDY].[City] WHERE [Name]=@Name");
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddInParameter("@Name", name, DbType.String, 50)
                            .ExecuteList(new CityEO().GetMapper(), tm);
        }

        /// <summary>
        /// 按自定义条件查询
        /// </summary>
        /// <param name = "where">自定义条件,where子句</param>
        /// <param name = "top">获取行数</param>
        /// <param name = "sort">排序表达式</param>
        /// <param name = "paras">where子句中定义的参数集合</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public List<CityEO> Select(string where, int top, string sort, IEnumerable<DbParameter> paras, TransactionManager tm = null)
        {
            var sql = new StringBuilder();
            var format = new StringBuilder();
            format.Append(@"SELECT {0} [CityId], [Code], [Name] FROM [LANDY].[City] ");
            if (!string.IsNullOrEmpty(where)) format.AppendFormat(" WHERE {0} ", where);
            if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
            sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
            return AdoBase(sql.ToString())
                            .AddParameters(paras)
                            .ExecuteList(new CityEO().GetMapper(), tm);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name = "where">条件表达式</param>
        /// <param name = "sort">排序表达式</param>
        /// <param name = "paras">传入参数</param>
        /// <param name = "pageSize">页大小</param>
        /// <param name = "pageIndex">页码</param>
        /// <param name = "recordCount">总记录数，由于计算总记录数影响数据库性能，只有小于或等于0的情况下才返回总记录数</param>
        /// <return>分页数据</return>
        public List<CityEO> SelectPager(string where, string sort, IList<DbParameter> paras, long pageSize, long pageIndex, ref long recordCount)
        {
            var sql = new StringBuilder();
            sql.AppendFormat(@"SELECT [CityId], [Code], [Name] FROM [LANDY].[City]");
            if (!string.IsNullOrEmpty(where)) sql.AppendFormat(" WHERE {0} ", where);
            if (!string.IsNullOrEmpty(sort)) sql.AppendFormat(" ORDER BY {0} ", sort);
            var pager = this.DataBase.CreatePager(sql.ToString(), pageSize, "CityId");
            pager.AddParameters(paras);
            if (recordCount <= 0)
            {
                recordCount = pager.GetRecordCount();
            }
            return pager.GetPageData(pageIndex).MapToList(new CityEO().GetMapper());
        }

        #endregion

        #region Insert方法集合
        /// <summary>
        /// 插入数据，有自增列时，item中自增字段为新增值，否则，返回影响的行数
        /// </summary>
        /// <param name="tm">事务管理对象</param>
        /// <param name = "item">要插入的实体对象</param>
        /// <return></return>
        public int Insert(CityEO item, TransactionManager tm = null)
        {
            const string sql = @"INSERT INTO [LANDY].[City] ([Code], [Name]) VALUES (@Code, @Name) SELECT SCOPE_IDENTITY();";
            item.CityId = int.Parse(AdoBase(sql)
                                                .AddInParameter("@Code", item.Code != null ? (object)item.Code : DBNull.Value, DbType.AnsiString, 10)
                                                .AddInParameter("@Name", item.Name != null ? (object)item.Name : DBNull.Value, DbType.String, 50)
                                                .ExecuteScalar(tm).ToString());
            return 1;
        }
        #endregion

        #region Update方法集合
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name = "item">要更新的实体对象</param>
        /// <param name = "tm">事务管理对象</param>
        /// <return></return>
        public int Update(CityEO item, TransactionManager tm = null)
        {
            const string sql = @"UPDATE [LANDY].[City] SET [Code]=@Code, [Name]=@Name WHERE [CityId]=@OriginalCityId";
            return AdoBase(sql)
                            .AddInParameter("@OriginalCityId", (item.HasOriginal) ? item.OriginalCityId : item.CityId, DbType.Int32, 4)
                            .AddInParameter("@Code", item.Code != null ? (object)item.Code : DBNull.Value, DbType.AnsiString, 10)
                            .AddInParameter("@Name", item.Name != null ? (object)item.Name : DBNull.Value, DbType.String, 50)
                            .ExecuteNonQuery(tm);
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name = "cityId"></param>
        /// <param name = "code"></param>
        /// <return></return>
        public virtual int UpdateCode(int cityId, string code)
        {
            return UpdateCode(cityId, code, null);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name = "tm">事务管理对象</param>
        /// <param name = "cityId"></param>
        /// <param name = "code"></param>
        /// <return></return>
        public virtual int UpdateCode(int cityId, string code, TransactionManager tm = null)
        {
            const string sql = @"UPDATE [LANDY].[City] SET [Code]=@Code  WHERE [CityId]=@CityId";
            return AdoBase(sql)
                            .AddInParameter("@Code", code != null ? (object)code : DBNull.Value, DbType.AnsiString, 10)
                            .AddInParameter("@CityId", cityId, DbType.Int32, 4)
                            .ExecuteNonQuery(tm);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name = "cityId"></param>
        /// <param name = "name"></param>
        /// <return></return>
        public virtual int UpdateName(int cityId, string name)
        {
            return UpdateName(cityId, name, null);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name = "tm">事务管理对象</param>
        /// <param name = "cityId"></param>
        /// <param name = "name"></param>
        /// <return></return>
        public virtual int UpdateName(int cityId, string name, TransactionManager tm = null)
        {
            const string sql = @"UPDATE [LANDY].[City] SET [Name]=@Name  WHERE [CityId]=@CityId";
            return AdoBase(sql)
                            .AddInParameter("@Name", name != null ? (object)name : DBNull.Value, DbType.String, 50)
                            .AddInParameter("@CityId", cityId, DbType.Int32, 4)
                            .ExecuteNonQuery(tm);
        }

        /// <summary>
        /// 按自定义条件更新数据
        /// </summary>
        /// <param name = "set">更新列</param>
        /// <param name = "where">更新条件</param>
        /// <param name = "paras">参数集合</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public int Update(string set, string where, IEnumerable<DbParameter> paras, TransactionManager tm = null)
        {
            if (string.IsNullOrEmpty(set)) throw new ArgumentNullException("set");
            var sql = string.Format("UPDATE [LANDY].[City] SET {0} {1}", set, (string.IsNullOrEmpty(where) ? string.Empty : " WHERE " + where));
            return AdoBase(sql)
                            .AddParameters(paras)
                            .ExecuteNonQuery(tm);
        }
        #endregion

        #region Delete方法集合
        /// <summary>
        /// 按主键删除
        /// </summary>
        /// <param name="tm">事务管理对象</param>
        /// <param name = "cityId"></param>
        public int DeleteByPK(int cityId, TransactionManager tm = null)
        {
            const string sql = @"DELETE FROM [LANDY].[City] WHERE [CityId]=@CityId";
            return AdoBase(sql)
                            .AddInParameter("@CityId", cityId, DbType.Int32, 4)
                            .ExecuteNonQuery(tm);
        }
        /// <summary>
        /// 按字段删除
        /// </summary>
        /// <param name="tm">事务管理对象</param>
        /// <param name = "code"></param>
        public int DeleteByCode(string code, TransactionManager tm = null)
        {
            const string sql = @"DELETE FROM [LANDY].[City] WHERE [Code]=@Code";
            return AdoBase(sql)
                            .AddInParameter("@Code", code != null ? (object)code : DBNull.Value, DbType.AnsiString, 10)
                            .ExecuteNonQuery(tm);
        }
        /// <summary>
        /// 按字段删除
        /// </summary>
        /// <param name="tm">事务管理对象</param>
        /// <param name = "name"></param>
        public int DeleteByName(string name, TransactionManager tm = null)
        {
            const string sql = @"DELETE FROM [LANDY].[City] WHERE [Name]=@Name";
            return AdoBase(sql)
                            .AddInParameter("@Name", name != null ? (object)name : DBNull.Value, DbType.String, 50)
                            .ExecuteNonQuery(tm);
        }
        /// <summary>
        /// 删除指定条件的记录
        /// </summary>
        /// <param name = "whereClause">自定义删除条件，where子句。如：id=1 and name='aaa'</param>
        /// <param name = "paras">where子句中定义的参数集合</param>
        /// <param name="tm">事务管理对象</param>
        /// <return></return>
        public int Delete(string whereClause, IEnumerable<DbParameter> paras, TransactionManager tm = null)
        {
            var sql = string.Format(@"DELETE FROM [LANDY].[City] {0}", (string.IsNullOrEmpty(whereClause) ? string.Empty : " WHERE " + whereClause));
            return AdoBase(sql)
                            .AddParameters(paras)
                            .ExecuteNonQuery(tm);
        }
        #endregion

        #region  自定义方法集合
        #endregion

    }


    #endregion
}
